package com.ychs.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ychs.bean.Clazz;
import com.ychs.bean.Role;
import com.ychs.bean.User;
import com.ychs.service.IRoleService;
import com.ychs.service.IUserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description: 描述【用户操作】
 * Copyright: Copyright (c) 2018
 * Company: 英才汇硕信息技术有限公司
 *
 * @author tianchuanjie
 * @version 1.0
 * @created 2019/1/30 9:31
 */
@Controller
public class UserController {

    private Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private IUserService userService;

    @Autowired
    private IRoleService roleService;

    /**
     * 访问用户信息操作jsp页面
     * @return
     */
    @GetMapping("/userInfoJsp")
    public String userInfo(){
        return "/admin/userInfo";
    }


    @GetMapping("/userInfo")
    public void  getUserInfo(int  page, int limit, HttpServletResponse resp, HttpServletRequest request){

        resp.setCharacterEncoding("UTF-8");
        ObjectMapper objectMapper = new ObjectMapper();
        //查询所有用户的信息
        List<User> userList = userService.queryListPage(limit*(page-1),limit);

        Map<String ,Object> map = new HashMap<>();
        //分页查询layui的参数
        map.put("msg","");
        map.put("code",0);

        int userCount = userService.queryCount();
        map.put("count",userCount);
        map.put("data",userList);
        PrintWriter out = null;

        try {
            out= resp.getWriter();
            out.print(objectMapper.writeValueAsString(map));
        } catch (IOException e) {
            e.printStackTrace();
        }
        out.flush();
        out.close();
    }
}
